package br.unifor.mia.olio.xml;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class ExecutionList implements Runnable {
	private final Logger logger = Logger.getLogger(getClass().getName());

	private final String content;

	private final static String TD_INI = "<td";
	private final static String TR_INI = "<tr";
	private final static String TR_FIM = "</tr>";

	private List<ExecutionModel> executions;

	public ExecutionList(String content) {
		executions = new ArrayList<ExecutionModel>();
		this.content = content;
	}
	
	public List<ExecutionModel> getExecutions(){
		
		return executions;
	}

	public static String loadUrl(String url) throws IOException {

		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(url);

		// Create a response handler
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = httpclient.execute(httpget, responseHandler);

		return responseBody;
	}

	public void run() {
		String tag = "";

		boolean newExecution = false;
		int sumColumns = 0;
		for (int i = 0; i < content.length(); i++) {
			String letter = content.substring(i, i + 1);

			tag += letter;

			if ("<".equals(letter)) {
				tag = "<";

			} else if (">".equals(letter)) {

				if (tag.indexOf(TR_INI) != -1) {
					executions.add(new ExecutionModel());
					sumColumns = 0;
					newExecution = true;

				} else if (newExecution) {
					if (tag.indexOf(TD_INI) != -1) {

						String innerTd = "";
						tdLoop: for (i = i + 1; i < content.length(); i++) {
							letter = content.substring(i, i + 1);
							innerTd += letter;

							if (">".equals(letter)) {

								if (innerTd.indexOf("</td>") != -1) {

									innerTd = innerTd.substring(0,
											innerTd.length() - 5);

									break tdLoop;
								}

							}

						}

						switch (sumColumns) {
						case 1:
							executions.get(executions.size() - 1).setName(
									innerTd);
							break;

						case 2:
							executions.get(executions.size() - 1)
									.setDescription(innerTd);
							break;
						case 3:
							executions.get(executions.size() - 1).setResult(
									innerTd);
							break;
						case 4:
							executions.get(executions.size() - 1).setUsers(
									innerTd);
							break;
						case 6:
							executions.get(executions.size() - 1).setDate(tag);
							break;
						}

						sumColumns++;

					}
				}

				if (sumColumns == 8) {
					newExecution = false;
				}

			}
		}
		
		executions.remove(0);
		
		for (ExecutionModel exec : executions) {
			String date = exec.getDate();
			
			int indexIni = date.indexOf("showtip('");
			int indexFim = date.indexOf(" ", indexIni);
			
			String strData = date.substring(indexIni+"showtip('".length(), indexFim+1);
			
			indexIni = date.indexOf(";", indexFim);
			indexFim = date.indexOf("&", indexIni);
			
			strData += date.substring(indexIni+1, indexFim);
			
			exec.setDate(strData);
			
			logger.finest(exec.toString());
		}
		
		logger.info(executions.size()+"");
	}

	public void iniHtml() {

	}

	public void fimHtml() {

	}

	public static void main(String[] args) throws IOException {
		FileReader reader = new FileReader(
				"C:\\Users\\mathcunha\\Desktop\\teste\\teste.htm");
		BufferedReader buffReader = new BufferedReader(reader);

		StringBuffer buffer = new StringBuffer();

		while (buffReader.ready()) {
			buffer = buffer.append(buffReader.readLine());
		}

		ExecutionList list = new ExecutionList(
				ExecutionList
						.loadUrl("http://192.168.2.104:9980/controller/results/list"));
		list.run();

	}

}
